package us.versus.them.weeds.demos;

import flash.display.GradientType;
import flash.display.Graphics;
import flash.display.InterpolationMethod;
import flash.display.MovieClip;
import flash.display.SpreadMethod;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Point;

import us.versus.them.weeds.Vertlet;
import us.versus.them.weeds.constraints.Angle;
import us.versus.them.weeds.constraints.Box;
import us.versus.them.weeds.constraints.Constraint;
import us.versus.them.weeds.constraints.Offset;
import us.versus.them.weeds.constraints.Segment;

class Dizzy extends Angularia {

	var prev_mouse:Vertlet;

	public static function main() {
		new Dizzy();
	}

	public function new( ?clip:MovieClip ) {
		super( clip );
	}

	public override function setup() {
		var w2 = this.clip.mouseX;
		var h2 = this.clip.mouseY;

		this.dx = this.dy = 60;

		this.level = 3;

		this.mouse =      new Vertlet( w2, h2, 10 );
		this.prev_mouse = new Vertlet( w2, h2, 10 );

		this.createSatellites( 64, 120, 1 );
		this.connectSatellitesTogether();
		this.connectSatellitesToPt( mouse );

		this.box = new Box( this.satellites, clip.stage.stageWidth, clip.stage.stageHeight );
		constraints.push( this.box );
	}

	public function connectSatellitesTogether() {
		var prv:Vertlet = null;
		var first:Vertlet = null;
		var s2s_rigidity   = 0.2;
		for ( pt in satellites ) {
			if ( null != prv ) {
			//	constraints.push( new Segment( prv, pt, s2s_rigidity ) );
				constraints.push( new Offset( prv, pt ) );
			}
			prv = pt;
			if ( null == first ) {
				first = pt;
			}

		}
		if ( null != first && null != prv ) {
			//constraints.push( new Segment( prv, first, s2s_rigidity ) );
			constraints.push( new Offset( prv, first ) );
		}
	}

	public function connectSatellitesToPt( mouse ) {
		//var m2s_rigidity = 1.5;
		for ( pt in satellites ) {
			constraints.push( new Offset( mouse, pt ) );
			//constraints.push( new Segment( mouse, pt, m2s_rigidity ) );
		}

	}

	public function createSatellites( n, r, satellite_mass ) {
		var w2 = this.clip.mouseX;
		var h2 = this.clip.mouseY;

		var ainc = 44.0 / 7.0 / n;
		var angle = 0.0;
		for ( i in 0 ... n ) {
			var pt = new Vertlet( 
				  w2 + Math.cos( angle ) * r
				, h2 + Math.sin( angle ) * r
				, satellite_mass
			);
			angle += ainc;

			satellites.push( pt );
		}
	}
}
